<template>
  <div class="hot-rank-detail" v-if="isVisible">
    <div class="detail-header">
      <div class="header-title">{{ title }}</div>
      <van-icon name="arrow-left" class="back-button" @click="handleBack" />
    </div>

    <div class="detail-content" v-if="rankList.length > 0">
      <div class="rank-item" v-for="(item, index) in rankList" :key="item.id" @click="handleItemClick(item)">
        <div class="rank-badge" :class="getRankBadgeClass(index)">
          No.{{ index + 1 }}
        </div>
        <div class="item-cover">
          <img :src="item.cover" alt="" />
        </div>
        <div class="item-info">
          <div class="item-title">{{ item.title }}</div>
          <div class="item-subtitle">{{ item.subtitle }}</div>
        </div>
      </div>
    </div>

    <div v-else class="loading">加载中...</div>

    <!-- 阅读详情组件 -->
    <ReadingDetail v-if="readingDetailVisible" :articleId="currentArticleId" :isVisible="readingDetailVisible"
      @close="closeReadingDetail" @article-change="handleArticleChange" />

    <!-- 问答详情组件 -->
    <QADetail v-if="qaDetailVisible" :articleId="currentQAId" :isVisible="qaDetailVisible" @close="closeQADetail" />
  </div>
</template>

<script>
import { getHotRankList } from '@/api/api'
import { Icon } from 'vant'
import ReadingDetail from './ReadingDetail.vue'
import QADetail from './QADetail.vue'

export default {
  components: {
    [Icon.name]: Icon,
    ReadingDetail,
    QADetail
  },
  name: 'HotRankDetail',
  props: {
    rankId: {
      type: [String, Number],
      required: true
    },
    title: {
      type: String,
      default: '排行榜'
    },
    isVisible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      rankList: [], // 当前排行榜的条目列表
      readingDetailVisible: false, // 是否展示阅读详情弹层
      qaDetailVisible: false, // 是否展示问答详情弹层
      currentArticleId: '', // 当前阅读详情的文章 ID
      currentQAId: '' // 当前问答详情的文章 ID
    }
  },
  watch: {
    // 监听排行榜 ID 变化，自动拉取新数据
    rankId: {
      handler(newId) {
        if (newId) {
          this.getRankListData(newId);
        }
      },
      immediate: true
    },
    // 当组件展示时，如果已有 ID，刷新数据
    isVisible(newVal) {
      if (newVal && this.rankId) {
        this.getRankListData(this.rankId);
      }
    }
  },
  methods: {
    // 请求排行榜详情数据
    getRankListData(id) {
      getHotRankList(id)
        .then(res => {
          if (res.data && res.data.length > 0) {
            this.rankList = res.data;
          } else {
            this.rankList = [];
          }
        })
        .catch(error => {
          console.error('获取排行榜详情失败:', error);
          this.rankList = [];
        });
    },
    // 根据排名返回对应徽章样式
    getRankBadgeClass(index) {
      if (index === 0) {
        return 'rank-first';
      } else if (index === 1) {
        return 'rank-second';
      } else {
        return 'rank-other';
      }
    },
    // 点击返回按钮关闭详情
    handleBack() {
      this.$emit('close');
    },
    // 根据榜单类型打开阅读或问答详情
    handleItemClick(item) {
      // 判断标题是否包含"阅读"或"问答"
      if (this.title && this.title.includes('阅读')) {
        this.openReadingDetail(item);
      } else if (this.title && this.title.includes('问答')) {
        this.openQADetail(item);
      }
    },
    // 打开阅读详情弹层
    openReadingDetail(item) {
      this.currentArticleId = item.id;
      this.readingDetailVisible = true;
    },
    // 关闭阅读详情弹层
    closeReadingDetail() {
      this.readingDetailVisible = false;
      this.currentArticleId = '';
    },
    // 处理阅读详情内部切换文章
    handleArticleChange(newArticleId) {
      this.currentArticleId = newArticleId;
    },
    // 打开问答详情弹层
    openQADetail(item) {
      this.currentQAId = item.id;
      this.qaDetailVisible = true;
    },
    // 关闭问答详情弹层
    closeQADetail() {
      this.qaDetailVisible = false;
      this.currentQAId = '';
    }
  }
}
</script>

<style scoped lang="less">
.hot-rank-detail {
  width: 100vw;
  height: 100vh;
  background-color: #fff;
  position: fixed;
  top: 0;
  left: 0;
  z-index: 100;
  box-sizing: border-box;
  display: flex;
  flex-direction: column;

  .detail-header {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 6vh;
    background-color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
    z-index: 1;
    box-sizing: border-box;

    .header-title {
      font-size: 14px;
      font-weight: 500;
      color: #333;
    }

    .back-button {
      position: absolute;
      left: 15px;
      top: 50%;
      transform: translateY(-50%);
      cursor: pointer;
      // font-size: 16px;
      color: #ccc;
      padding: 5px;
    }
  }

  .detail-content {
    width: 100%;
    flex: 1;
    height: calc(100vh - 7vh);
    margin-top: 6vh;
    padding: 0 10px;
    box-sizing: border-box;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;

    .rank-item {
      display: flex;
      align-items: center;
      gap: 4vw;
      padding: 1.5vh 0;
      position: relative;
      cursor: pointer;

      .rank-badge {
        position: absolute;
        top: 2vh;
        left: 0;
        width: 30px;
        height: 10px;
        border-top-left-radius: 4px;
        border-bottom-right-radius: 4px;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 10px;
        font-weight: 500;
        color: #fff;
        z-index: 2;

        &.rank-first {
          background-color: rgb(235, 1, 1);
        }

        &.rank-second {
          background-color: rgb(243, 98, 1);
        }

        &.rank-other {
          background-color: rgb(233, 198, 0);
        }
      }

      .item-cover {
        height: 7vh;
        aspect-ratio: 1/1;
        border-radius: 4px;
        overflow: hidden;
        flex-shrink: 0;

        img {
          width: 100%;
          height: 100%;
          object-fit: cover;
        }
      }

      .item-info {
        height: 7vh;
        flex: 1;
        display: flex;
        flex-direction: column;
        justify-content: space-between;
        min-width: 0;
        padding-top: 2px;
        padding-bottom: 2vw;
        box-sizing: border-box;

        .item-title {
          font-size: 13px;
          font-weight: 500;
          color: #333;
          margin-bottom: 5px;
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
        }

        .item-subtitle {
          font-size: 11px;
          color: #999;
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
        }
      }
    }
  }

  .loading {
    text-align: center;
    padding: 50px 20px;
    font-size: 14px;
    color: #999;
  }
}
</style>
